package xyz.fefine;

import java.util.List;

/**
 * Created by feng_ on 2016/9/8.
 */
public class WhileStmnt extends ASTList {
    public WhileStmnt(List<ASTree> list) {
        super(list);
    }
    public ASTree condition(){
        return child(0);
    }

    public BlockStmnt body(){
        return (BlockStmnt)child(1);
    }

    @Override
    public String toString() {
        return "( while " + condition() +" "+ body() + ")";
    }

    @Override
    public Object eval(Enviorment enviorment) {
        Object con = condition().eval(enviorment);
        Object res = null;
//        if (res instanceof Integer && ((Integer) res).intValue() == TRUE) {
//            return body().eval(enviorment);
//        }
        while (con instanceof Integer && ((Integer) con).intValue() == TRUE) {
            res = body().eval(enviorment);
            con = condition().eval(enviorment);
        }
        return res;
    }
}
